package com.shierke.Ume.ui.fragment.explore

import ViewPagerAdapterLazyFragment
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.shierke.Ume.R
import com.shierke.Ume.ui.activity.explore.SearchHistoryAndHotActivity
import com.shierke.Ume.ui.view.HXLinePagerIndicator
import com.shierke.Ume.util.click
import kotlinx.android.synthetic.main.fragment_expore.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

/**
 * Create by Ybx
 * on 2020-10-12
 * Desribe:
 */
class ExploreFragment : ViewPagerAdapterLazyFragment() {
    override fun onFragmentShow() {

    }

    override fun onFragmentHide() {
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view =
            LayoutInflater.from(activity!!).inflate(R.layout.fragment_expore, null, false)
        return view
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)



        seatch.click {
            SearchHistoryAndHotActivity.startActivity(activity!!)
        }

        titles.add(getString(R.string.explore_follow))
        titles.add(getString(R.string.explore_trend))
        titles.add(getString(R.string.explore_latest))

        fragments.add(ExploreFollowFragment())
        fragments.add(ExploreTrendFragment())
        fragments.add(ExploreLatestFragment())


        val viewpagerAdapter = ViewpagerAdapter(childFragmentManager)
        exploreViewPager.adapter = viewpagerAdapter


        var commonNavigator = CommonNavigator(activity)
        //commonNavigator.isAdjustMode = true//设置title宽度自适应
        commonNavigator.isEnablePivotScroll = true

        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(context)
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = UIUtil.dip2px(context, 20.0).toFloat()
                clipPagerTitleView.textColor = Color.parseColor("#9C9C9C")
                clipPagerTitleView.clipColor = Color.parseColor("#2C2C2C")


                clipPagerTitleView.click {

                    if (exploreViewPager != null) {
                        exploreViewPager.currentItem = index
                    }
                }


                return clipPagerTitleView

            }

            override fun getCount() = titles.size

            override fun getIndicator(context: Context?): IPagerIndicator? {

                val indicator = HXLinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 40.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                return indicator
            }
        }

        indicator.navigator = commonNavigator
        ViewPagerHelper.bind(indicator, exploreViewPager)


    }


    private var titles = arrayListOf<String>()
    private var fragments = arrayListOf<Fragment>()

    inner class ViewpagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getCount() = fragments.size

        override fun getItem(position: Int) = fragments[position]

        override fun getPageTitle(position: Int): CharSequence? {
            return titles[position]
        }
    }


}